APM.ConfigGlobalalarmsController = Ember.Controller.extend({
	init : function() {
		this._super();
		this.initConfData();
	},
	form : {
		confType : "5",
		snmpIp : "",
		snmpPort : "",
		snmpOn : false,
		alarmType : "1"
	},

	alarmTypes : Ember.A([ {
		"displaytext" : "阈值告警",
		"value" : "1"
	} ]),

	inpDisabled : Ember.computed("form.snmpOn", function() {
		return !this.get("form.snmpOn");
	}),

	// 禁用远程inp输入框
	snmpOnChange : Ember.observer("form.snmpOn", function() {
		var alarmEnableVal = this.get("form.snmpOn");
		if (!alarmEnableVal) {
			this.set("form.snmpIp", "");
			this.set("form.snmpPort", "");
		}
	}),
	
	divShowDisabled : Ember.computed("form.alarmType", function() {
		return this.get("form.alarmType") != '1';
	}),
	
	checkFormValid : function() {
		var form = this.get("form");

		this.set("saveResult", null);
		
		var reg1 = new RegExp("^[0-9]*$");

		if(this.get("form.snmpOn")) {
			if (!form.snmpIp) {
				alertTip("IP地址不能为空!");
					return false;
			}
			
			if (!form.snmpPort) {
				alertTip("端口不能为空!");
					return false;
			}
		}
		
		if (form.snmpPort) {
			var port=form.snmpPort;
			var port1=parseInt(form.snmpPort);
			if (!(reg1.test(port) && port1<65536)) {
				alertTip("端口填写错误!");
				return false;
			}
		}
		if (form.snmpIp) {
			var reg=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if (!reg.test(form.snmpIp)) {
				alertTip("IP地址非法，请重新输入!");
				return false;
			}
		}
		this.set("formTip", null);
		return true;
	},

	initConfData : function() {
		var ctrl = this;
		var form = this.get("form");
		
			ctrl.store.ajax("GET", "configs/systemConfig", {
				configItem : "integrate"
			}).then(function(response) {
						var configobj1 = response.configData;
						if (configobj1) {	
							if (configobj1.integrate_alert_trap_enabled=="1") {
								ctrl.set("form.snmpOn", true);
							} else {
								ctrl.set("form.snmpOn",false);
							}
							if (configobj1.integrate_alert_port) {
								ctrl.set("form.snmpPort",configobj1.integrate_alert_port);
							}
							if (configobj1.integrate_alert_ip) {
								ctrl.set("form.snmpIp", configobj1.integrate_alert_ip);
							}
						}
			});
	},

	actions : {
		reset : function() {
			this.set("formTip", null);
			this.set("saveResult", null);
		},
		
		save : function() {
			if (!this.checkFormValid()) {
				return;
			}
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("saveResult", null);
			ctrl.store.ajax("POST", "configs/systemConfig", {
				alarmType : form.alarmType,
				snmpIp : form.snmpIp,
				snmpPort :form.snmpPort,
				snmpOn : String(Number(form.snmpOn)),
				confType : form.confType
			}).then(function(response) {
				if(response.status=="success"){
					//todo: 保存成功逻辑
					// ctrl.set("saveResult", "保存成功！");
					alertTip("保存成功");
				}else{
					alertTip(response.message);
					// ctrl.set("saveResult", response.message);
				}
			}, function() {
				alertTip("保存失败！");
				// ctrl.set("saveResult", "保存失败！");
			});
		}
	}
});